package com.demo.oauth.config;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author yangjian
* @version 创建时间：2022年10月18日 下午5:52:25
* 
*/
@Configuration
public class RedissonConfig {
	
	@Autowired
	private RedisProperties redisProperties;
	
	@Bean
	public RedissonClient redissonClient() {
		Config config = new Config();
		Cluster cluster = redisProperties.getCluster();
		Sentinel sentinel = redisProperties.getSentinel();
		//集群模式
		if (null != cluster && null != cluster.getNodes() && cluster.getNodes().size() > 0) { // cluster的nodes节点不为空，说明启用的是redis集群配置
			List<String> list = redisProperties.getCluster().getNodes();
			String[] nodes = new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
				String[] arrs = list.get(i).split(":");
				String address = "redis://%s:%s";
				address = String.format(address, arrs[0].trim(), arrs[1].trim());
				nodes[i] = address;
			}
			ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(nodes);
			if (StringUtils.isNotBlank(redisProperties.getPassword())) {
				clusterServersConfig.setPassword(redisProperties.getPassword());
			}
			return Redisson.create(config);
		}else if(null != sentinel && null != sentinel.getNodes() && sentinel.getNodes().size() > 0) {
			//哨兵模式
			List<String> list = redisProperties.getSentinel().getNodes();
			SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
			sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
			String[] nodes = new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
				String[] arrs = list.get(i).split(":");
				String address = "redis://%s:%s";
				address = String.format(address, arrs[0].trim(), arrs[1].trim());
				nodes[i] = address;
			}
			sentinelServersConfig.addSentinelAddress(nodes);
			if (StringUtils.isNotBlank(redisProperties.getPassword())) {
				sentinelServersConfig.setPassword(redisProperties.getPassword());
			}
			sentinelServersConfig.setDatabase(redisProperties.getDatabase());
			return Redisson.create(config);
		} else { //单机模式
			String address = "redis://%s:%s";
			address = String.format(address, redisProperties.getHost(), redisProperties.getPort());
			SingleServerConfig sc = config.useSingleServer().setAddress(address);
			if (StringUtils.isNotBlank(redisProperties.getPassword())) {
				sc.setPassword(redisProperties.getPassword());
			}
			sc.setDatabase(redisProperties.getDatabase());
			return Redisson.create(config);
		}
	}


}
